package mysql

import (
	"short-video-manager/videorpc/videorpc"

	"gorm.io/gorm"
)

type VideoInfo struct {
	gorm.Model
	UserId        int64  `gorm:"type:int(11);not null;comment:用户ID"`
	VideoTitle    string `gorm:"type:varchar(500);not null;comment:视频标题"`
	VideoCategory string `gorm:"type:varchar(30);not null;comment:视频分裂"`
	VideoLink     string `gorm:"type:varchar(300);not null;comment:视频URl"`
	CoverLine     string `gorm:"type:varchar(300);not null;comment:视频封面URL"`
	VideoIntro    string `gorm:"type:varchar(500);comment:视频简介"`
	PlayNym       int    `gorm:"type:int;comment:播放量"`
	LikeNum       int    `gorm:"type:int;comment:点赞量"`
	CollectionNum int    `gorm:"type:int;comment:收藏量"`
	CommentNum    int    `gorm:"type:int;comment:收藏量"`
	VideoStatus   int    `gorm:"type:int(11);comment:发布状态(0-已发布，1-草稿，2-下架)"`
	TopicId       int    `gorm:"type:int;comment:关联话题ID"`
}

func (v *VideoInfo) VideoAdd(db *gorm.DB) error {
	return db.Debug().Create(v).Error
}

func (v *VideoInfo) FindVideoById(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).First(v).Error
}

func (v *VideoInfo) UpdateVideo(db *gorm.DB) error {
	return db.Debug().Updates(v).Error
}

func (v *VideoInfo) VideoDel(db *gorm.DB) error {
	return db.Debug().Delete(v).Error
}

func (v *VideoInfo) VideoShow(db *gorm.DB, id int64) videorpc.VideoShowResponse {
	var show videorpc.VideoShowResponse
	db.Debug().Model(v).Select("video_infos.id as video_id,video_title,video_category,video_link,nick_name,url").
		Joins("LEFT JOIN user_infos ON video_infos.user_id = user_infos.id").Where("video_infos.id=?", id).Find(&show)
	return show
}

func VideoList(db *gorm.DB, in *videorpc.VideoListRequest) []*videorpc.VideoList {
	var list []*videorpc.VideoList
	db.Debug().Model(&VideoInfo{}).Select("video_infos.id as video_id,video_title,video_category,video_link,nick_name,play_nym as play_num").
		Joins("LEFT JOIN user_infos ON video_infos.user_id = user_infos.id").
		Order("play_nym desc").Limit(3).Scopes(Paginate(int(in.Page), int(in.Size))).Find(&list)
	return list
}
